#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <iterator>

using namespace std;
//range[] stands for position
void count_sort(int unsort[],int sort[],int length,int k)
{
  int* range = (int*)calloc(k,sizeof(int));

  //set counter array range
  for(int i=0;i<length;++i) 
    range[unsort[i] ]++;

  //calculate its position initially
  for(int i=1;i<k;i++) //loop for range[]
    range[i] +=range[i-1];
  for(int i=k-1;i>=0;i--)//shift right-->
    range[i] = range[i-1];
  range[0] = 0;

  //from 0 upto length-1
  for(int i=0;i<length;i++) {//loop for unsort[],sort[]
    sort[range[unsort[i]]] = unsort[i];
    range[unsort[i]]++;
  }
}

int main()
{
  int unsort[10] = {3,4,3,1,2,6,5,2,8,0};
  int sort[10];
  count_sort(unsort,sort,10,9);

  copy(sort,sort+10,ostream_iterator<int>(cout," ") );
  cout<<endl;
  return 0;
}
  
  
